package com.swfee.qinben;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.swfee.qinben.base.DoPickMultimediaBaseActivity;
import com.swfee.qinben.dto.req.BindEquipementReqDto;
import com.swfee.qinben.dto.req.HealthMeasureReqDto;
import com.swfee.qinben.dto.rsp.HealthMeasureRspDto;
import com.swfee.qinben.dto.rsp.ResultRspDto;
import com.swfee.qinben.httpactions.ScheduleAction;
import com.swfee.qinben.util.CurenActivitUtils;

public class ScheduleOperBindAdviceActivity extends DoPickMultimediaBaseActivity {


	private LayoutInflater inflater;
	private BindEquipementReqDto bindEquipementReqDto;
	ScheduleAction scheduleAction = new ScheduleAction();
	
	Handler handle = new Handler();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setContentView(R.layout.schedule_opt_bind_device);
		
		 bindEquipementReqDto = getIntent().getExtras().getParcelable("bindEquipementReqDto");
		
		Button submitComment = (Button) findViewById(R.id.btn_bind_test_finish);
	
		submitComment.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				doSynchDeviceResult();

			}
		});

//		ImageView uploadFile = (ImageView) findViewById(R.id.upload_file);
//
//		uploadFile.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				doPickDoAction();
//			}
//		});
	}
	
	//同步服务器状态
	//调用ProcessTaskStatus
	public void doSynchDeviceResult(){
		showProgressDialog("同步设备测量结果，请等待服务器通知..");
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				final ResultRspDto rspDto  = scheduleAction.processTaskStatus(bindEquipementReqDto.getEquipmentId(), 1);
				handle.post(new Runnable() {
					
					@Override
					public void run() {
						int resultInt = Integer.parseInt(rspDto.getResult());
						if(resultInt==1){
							showProgressDialog("同步设备结果成功,获取测量结果中..");
							new Thread(new Runnable() {

								@Override
								public void run() {
									HealthMeasureReqDto hmReqDto = new HealthMeasureReqDto();
									hmReqDto.setEquipmentId(bindEquipementReqDto.getEquipmentId());
									hmReqDto.setFamilyId(bindEquipementReqDto.getFamilyId());
									hmReqDto.setScheduleId(bindEquipementReqDto.getScheduleId());
									final HealthMeasureRspDto heqRspDto =  scheduleAction.getHealthMeasureData(hmReqDto);
									
									handle.post(new Runnable() {
										
										@Override
										public void run() {
											hideProgressDialog();
											Bundle bu = getIntent().getExtras(); 
											bu.putString("measureData", heqRspDto.getMeasureData());
											bu.putString("solution", heqRspDto.getSolution());
											bu.putInt("healthResult", heqRspDto.getHealthResult());
											CurenActivitUtils.startActivity(ScheduleOperSubmitActivity.class, true,bu);
										}
									});
									
									
								}
								
								
							}).start();
						}
					}
				});
			}
		}).start();
	}
	

	public String getResultMsgByResult(int result){
		switch (result) {
		case 0:
			return "状态更新失败";
		case 1:
			return "状态更新成功";
		case 2:
			return "服务器未接收到数据，状态更新失败";
		case 3:
			return "提示测量数据处于非正常范围，状态更新成功";
		
		}
		return "未知状态";
	}
}